home *** CD-ROM | disk | FTP | other *** search
/ Aminet 38 / Aminet 38 (2000)(Schatztruhe)[!][Aug 2000].iso / Aminet / gfx / misc / FujiControl.lha / primitives.c < prev    next >
Encoding:
Text File  |  1999-06-06  |  8.9 KB  |  357 lines

  1. /************ Camera specific primitives ************/
  2. attention()
  3. {
  4.     int i=0;
  5.     unsigned char character;
  6.  
  7.     while (readserial(&character,1) && i++ <200);
  8.  
  9.     for (i=0; i<5; i++)
  10.     {
  11.         if (debug & 8) printf("TX>>ENQ (attention)\n");
  12.         sendchar(0x05);
  13.         Delay(5);
  14.         if (readserial(&character,1) && character == 0x06) 
  15.         {    
  16.             if (debug & 8) printf("RX<<ACK\n");
  17.             return(1);
  18.         }
  19.     }
  20.     return(0);
  21. }
  22.         
  23. senddata(unsigned char *data,char *command,int length)
  24. {
  25.     unsigned char bcc=0;
  26.     int i;
  27.     char buff[255];
  28.     int chars_sent = 0;
  29.     int packetlength;
  30.     int lastpacket=0;
  31.     int resplength=0;
  32.  
  33.     /* Send the picture */
  34.     while (chars_sent<length && !lastpacket)
  35.     {    
  36.         /* prepare the size indicators in the command */
  37.         packetlength = length - chars_sent;
  38.         if (packetlength >512) packetlength = 512; else lastpacket=1;
  39.         command[2] = packetlength & 0xff;
  40.         command[3] = packetlength >> 8;            
  41.  
  42.         /* calculate checksum */
  43.         bcc=0;
  44.         for(i=0; i<packetlength; i++)
  45.             bcc = bcc ^ data[i+chars_sent];
  46.         for (i=0; i<4; i++)
  47.             bcc =bcc ^ command[i];
  48.         if (lastpacket) bcc = bcc ^ 0x03;
  49.         else bcc = bcc ^ 0x17;
  50.     
  51.         /* send the STX */
  52.         sendchar(0x10);
  53.         sendchar(0x02);
  54.     
  55.         /* send the command */
  56.         for (i=0; i<4; i++)
  57.         {
  58.             if (command[i] == 0x10) sendchar(0x10);
  59.             sendchar(command[i]);
  60.         }    
  61.         printhex("Cmd > ",command,4);
  62.  
  63.  
  64.         /* send the data */
  65.         for (i=0; i<packetlength; i++)
  66.         {
  67.             if (data[i+chars_sent] == 0x10) sendchar(0x10);
  68.             sendchar(data[i+chars_sent]);
  69.         }    
  70.         printhex("Data> ",&(data[chars_sent]),16);
  71.     
  72.         /* send the ETX or ETB */
  73.         sendchar(0x10);
  74.         if (lastpacket) sendchar(0x3); else sendchar(0x17);
  75.         if (lastpacket) printf("ETX\n"); else printf("ETB\n");    
  76.  
  77.         /* send the BCC */
  78.         sendchar(bcc);
  79.     
  80.         /* wait for the acknowledge */
  81.         for (i=0; i<80 && readserial(buff,1)!=1; i++) Delay(5);
  82.     
  83.         if (i == 10)
  84.             return( 0 * printf("!! no ACK received\n") );
  85.     
  86.         if(buff[0] == 0x15) 
  87.             return( 0 * printf("RX<<NACK!\n") );
  88.     
  89.         if (buff[0] != 0x06)
  90.             return( 0 * printf("RX<< %02x instead of ACK\n") );    
  91.     
  92.         if (buff[0]==0x6) printf("RX<<ACK\n");
  93.  
  94.         chars_sent += packetlength;
  95.         printf("  Sent %d of %d\n",chars_sent,length);
  96. #ifdef bongaloid
  97.         /* receive the response */
  98.         resplength=receivepacket(buff,4);
  99.  
  100.         printhex("Resp<  ",buff,resplength);
  101.  
  102.         /* send an ACK to the response */
  103.         sendchar(0x06);
  104. #endif    
  105.  
  106.     }
  107.     Delay(50);
  108.     flushserial(buff,100,1);
  109.     return(1);    
  110. }
  111.  
  112.     
  113. sendpacket(unsigned char *message,int length)
  114. {
  115.     unsigned char bcc=0;
  116.     int i;
  117.     char buff[10];
  118.     
  119.     if (debug & 2)    printhex("TX>>",message,length);
  120.     if (debug & 4)    printascii("TX>>",message,length);
  121.  
  122.     /* calculate checksum */
  123.     for(i=0; i<length; i++)
  124.         bcc = bcc ^ message[i];
  125.     bcc = bcc ^ 0x03;
  126.  
  127.     /* send the STX */
  128.     sendchar(0x10);
  129.     sendchar(0x02);
  130.  
  131.     /* send the data */
  132.     for (i=0; i<length; i++)
  133.     {
  134.         if (message[i] == 0x10) sendchar(0x10);
  135.         sendchar(message[i]);
  136.     }    
  137.  
  138.     /* send the ETX */
  139.     sendchar(0x10);
  140.     sendchar(0x03);
  141.  
  142.     /* send the BCC */
  143.     sendchar(bcc);
  144.  
  145.     /* wait for the acknowledge */
  146.     for (i=0; i<40 && readserial(buff,1)!=1; i++) Delay(5);
  147.  
  148.     if (i == 10)
  149.         return( 0 * printf("!! no ACK received\n") );
  150.  
  151.     if(i!=10 && buff[0] == 0x15) 
  152.         return( 0 * printf("RX<<NACK!\n") );
  153.  
  154.     if (i!=10 && buff[0] != 0x06)
  155.         return( 0 * printf("RX<< %02x instead of ACK\n") );    
  156.  
  157.     if (debug & 8) printf("RX<<ACK\n");
  158.     return(1);    
  159. }
  160.  
  161.             
  162.  
  163. receivepacket(unsigned char *buffer, int maxlen)
  164. {
  165.     enum
  166.     {    
  167.         rxs_waitforSTX1     =  0,    /* waiting for the escape preceeding the STX */
  168.         rxs_waitforSTX2     = 10,    /* waiting for STX after having received the preceeding escape */
  169.         rxs_waitforHeader    = 12,    /* waiting for header bytes */
  170.         rxs_waitforHCtrl    = 14,    /* waiting for a control character after escape received in header */
  171.         rxs_waitforData     = 20,    /* waiting for an ordinary data byte */
  172.         rxs_waitforCtrl     = 30,    /* waiting for a control character after escape received in data */
  173.         rxs_waitforBCC      = 40,    /* waiting for BCC after an ETB was recevied */
  174.         rxs_Ready             = 50,    /* read all there is to read */
  175.         rxs_waitforETB_BCC     = 60,    /* wait for the BCC following an ETB */
  176.         rxs_RX_Debug         = 1024    /* receive characters until buffer is empty */
  177.     };
  178.  
  179.     int idlecycles = 0;            /* number of intervals since last character received */
  180.     int timeout = 60;            /* ie 20 idlecycles */
  181.     int cycletime = 5;            /* duration of each wait cycle */
  182.  
  183.     int state=rxs_waitforSTX1;    /* current receiving state (see enum above) */
  184.     
  185.     int header_chars = 0;        /* number of header chars received (there should be 4 in each camera response) */
  186.     unsigned char headchar[4];    /* the header characters received */    
  187.  
  188.     int chars_received = 0;        /* number of characters received and accepted*/
  189.     int chars_skipped = 0;        /* number of unexpected characters skipped */
  190.     int packets_received=0;        /* number of good packets received */
  191.     
  192.     unsigned char bcc=0,bccrx;    /* the block check character (checksum of sorts) calculated and received values */
  193.  
  194.     unsigned char character;        /* the currently received character */
  195.     int dispcount=999;            /* counter to display packet count every 20 or so */
  196.     unsigned char a;            /* just a temporary var */
  197.     int chars;                /* anothr one */
  198.  
  199.     while ( idlecycles < timeout && state != rxs_Ready)
  200.     {
  201.         if (!serchars())
  202.         {
  203.             idlecycles++;
  204.             Delay(cycletime);
  205.             if (debug & 1) printf("    Idle cycles = %03d\n",idlecycles);
  206.         }
  207.         else
  208.         {
  209.             idlecycles=0;
  210.             if (debug & 1) printf("    From state %02d --> ",state);
  211.             switch(state)
  212.             {
  213.                 case rxs_waitforSTX1:
  214.                     readserial(&character,1);
  215.                     if ( character == 0x10) state = rxs_waitforSTX2;
  216.                     else chars_skipped++;
  217.                     break;
  218.                 case rxs_waitforSTX2:
  219.                     readserial(&character,1);
  220.                     if ( character == 0x02)
  221.                     {
  222.                         state = rxs_waitforHeader;
  223.                         header_chars=0;
  224.                     }
  225.                     else { chars_skipped++; state = rxs_waitforSTX1; }
  226.                     break;
  227.                 case rxs_waitforHeader:
  228.                     readserial(&character,1);
  229.                     if ( character == 0x10 ) state = rxs_waitforHCtrl;
  230.                     else
  231.                     {
  232.                         bcc = bcc ^ character;
  233.                         headchar[header_chars]=character;
  234.                         header_chars++;
  235.                         if (header_chars== 4)
  236.                         {
  237.                             if (debug & 16)    printhex("RX<< Header:",headchar,4);
  238.                             header_chars=0;
  239.                             state=rxs_waitforData;
  240.                         }
  241.                     }
  242.                     break;
  243.                 case rxs_waitforHCtrl:
  244.                     readserial( &character,1);
  245.                     switch(character)
  246.                     {
  247.                         case 0x03:
  248.                             bcc = bcc ^ character;
  249.                             state = rxs_waitforBCC;
  250.                             break;
  251.                         case 0x17:
  252.                             bcc = bcc ^ character;
  253.                             state = rxs_waitforETB_BCC;
  254.                             break;
  255.                         case 0x10:
  256.                             headchar[header_chars]=character;
  257.                             bcc = bcc ^ character;
  258.                             header_chars++;
  259.                             state = rxs_waitforHeader;
  260.                             break;
  261.                         default:
  262.                             printf("!! Character %02x received after escape in header block\n",a=character);
  263.                             return(0);
  264.                     }
  265.                     break;
  266.                 case rxs_waitforData:
  267.                     readserial( &character,1);
  268.                     if ( character == 0x10 ) state = rxs_waitforCtrl;
  269.                     else
  270.                     {
  271.                         buffer[chars_received] = character;
  272.                         chars_received++;
  273.                         if (chars_received >= maxlen) 
  274.                         {
  275.                             printf("!! Received data exceeds allocated space\n");
  276.                             return(0);
  277.                         }
  278.                         bcc = bcc ^ character;
  279.                     }
  280.                     break;
  281.                 case rxs_waitforCtrl:
  282.                     readserial( &character,1);
  283.                     switch(character)
  284.                     {
  285.                         case 0x03:
  286.                             bcc = bcc ^ character;
  287.                             state = rxs_waitforBCC;
  288.                             break;
  289.                         case 0x17:
  290.                             bcc = bcc ^ character;
  291.                             state = rxs_waitforETB_BCC;
  292.                             break;
  293.                         case 0x10:
  294.                             buffer[chars_received] = character;
  295.                             bcc = bcc ^ character;
  296.                             chars_received++;
  297.                             state = rxs_waitforData;
  298.                             break;
  299.                         default:
  300.                             printf("!! Character %02x received after escape in data block\n",a=character);
  301.                             return(0);
  302.                     }
  303.                     break;
  304.                 case rxs_waitforBCC:
  305.                     readserial( &character,1);
  306.                     bccrx=character;
  307.                     state=rxs_Ready;
  308.                     break;
  309.                 case rxs_waitforETB_BCC:
  310.                     readserial( &character,1);
  311.                     bccrx=character;
  312.                     if (bccrx!=bcc) {printf("!!calculated bcc %d instead of %d\n",bcc,bccrx); break;};
  313.                     if (debug & 8) printf("TX>>ACK\n");
  314.                          if (dispcount ++ >18)
  315.                     {    
  316.                         dispcount=0;
  317.                         if ((debug & 64) && packets_received<1) printf("\n");
  318.                         if (debug & 64) printf("\x0b  Received %4d packets of %d \n",packets_received,maxlen/512);
  319.                     }
  320.                     packets_received++;
  321.                     sendchar(0x6);
  322.                     bcc=0;
  323.                     state=rxs_waitforSTX1;
  324.                     break;
  325.                 case rxs_RX_Debug:
  326.                     chars=readserial(buffer,MAXLEN);
  327.                     printhex(buffer,chars);
  328.                     state=rxs_RX_Debug;
  329.                     break;
  330.             }
  331.             if (debug & 1) printf(" state %02d by char %02x\n",state,character);    
  332.         }
  333.     }
  334.     if (debug & 2)    printhex("RX<<",buffer,chars_received);
  335.     if (debug & 4)    printascii("RX<<",buffer,chars_received);
  336.     
  337.     if (idlecycles>=timeout)
  338.     {
  339.         printf("!!Connection timed out in state %d\n",state);
  340.         return(0);
  341.     }
  342.  
  343.     /* check agreement of the checksum */
  344.     if (bccrx != bcc)
  345.     {
  346.         printf("!!Calculated BCC is %02x instead of received value of %02x\n",bcc,bccrx);
  347.         return(0);
  348.     }
  349.     else if (debug & 1) printf("    Checksum OK (%d)\n",bcc);
  350.  
  351.     
  352.     /* Acknowledge the received packet */
  353.     if (debug & 8) printf("TX>>ACK\n");
  354.     sendchar(0x6);
  355.     return(chars_received);
  356. }
  357.